<template>
  <div class="ongoing-exams">
    <StudentNavbar title="正在进行的考试" /> 
    
    <div class="page-header">
      <h1>正在进行的考试</h1>
      <p>以下是你当前正在参加的考试科目</p>
    </div>
    
    <div v-if="loading" class="loading">
      <el-spinner></el-spinner>
      <p>正在加载考试信息...</p>
    </div>
    
    <div v-else-if="error" class="error">
      <i class="el-icon-warning"></i>
      <p>{{ error }}</p>
    </div>
    
    <div v-else-if="examings.length === 0" class="no-exams">
      <i class="el-icon-document"></i>
      <p>暂无正在进行的考试</p>
    </div>
    
    <div v-else class="exams-list">
      <div
        v-for="exam in examings"
        :key="exam.subjectId"
        class="exam-item"
        @click="goToTakeExam(exam.subjectId)"
      >
        <div class="exam-info">
          <h3 class="exam-title">{{ exam.subjectName }}</h3>
          <div class="exam-details">
            <p><i class="el-icon-clock"></i> 限时: {{ exam.timeLimit }}分钟</p>
            <p><i class="el-icon-edit-outline"></i> 选择题: {{ exam.choiceQuestionCount }}题</p>
            <p><i class="el-icon-help"></i> 判断题: {{ exam.judgmentQuestionCount }}题</p>
            <p><i class="el-icon-edit"></i> 填空题: {{ exam.fillQuestionCount }}题</p>
          </div>
        </div>
        <div class="exam-actions">
          <el-button type="primary" size="small">继续考试</el-button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import StudentAPI from '@/api/student'
import StudentNavbar from '@/components/StudentNavbar.vue'

export default {
  name: 'OngoingExams',
  components: {
    StudentNavbar
  },
  data() {
    return {
      examings: [],
      loading: false,
      error: null
    }
  },
  created() {
    this.fetchExamings()
  },
  methods: {
    async fetchExamings() {
      this.loading = true
      this.error = null
      try {
        const res = await StudentAPI.getExamings()
        if (res.data.code === 200 && res.data.msg === 'success') {
          this.examings = res.data.data || []
        } else {
          this.error = '获取正在进行的考试失败: ' + (res.data.message || '未知错误')
        }
      } catch (error) {
        this.error = '获取正在进行的考试时发生网络错误'
      } finally {
        this.loading = false
      }
    },
    goToTakeExam(subjectId) {
      // 根据经验教训，点击参加考试时应先显示考试信息，再通过"开始考试"按钮调用takeExam接口
      this.$router.push(`/student/take-exam/${subjectId}`)
    }
  }
}
</script>

<style scoped>
.ongoing-exams {
  padding: 20px;
  background: linear-gradient(135deg, #1e3c72 0%, #2a5298 100%);
  min-height: calc(100vh - 40px);
}

.page-header {
  text-align: center;
  margin-bottom: 30px;
  padding: 20px;
}

.page-header h1 {
  color: #fff;
  font-size: 2.2rem;
  font-weight: 600;
  margin-bottom: 10px;
  text-shadow: 0 2px 4px rgba(0,0,0,0.3);
}

.page-header p {
  color: rgba(255, 255, 255, 0.85);
  font-size: 1.2rem;
}

.loading, .error, .no-exams {
  text-align: center;
  padding: 40px;
  color: white;
}

.loading i {
  font-size: 48px;
  margin-bottom: 20px;
}

.error i {
  font-size: 48px;
  color: #f56c6c;
  margin-bottom: 20px;
}

.no-exams i {
  font-size: 48px;
  color: #909399;
  margin-bottom: 20px;
}

.exams-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: 20px;
}

.exam-item {
  background: rgba(255, 255, 255, 0.1);
  backdrop-filter: blur(10px);
  border-radius: 15px;
  padding: 20px;
  display: flex;
  cursor: pointer;
  transition: all 0.3s ease;
  border: 1px solid rgba(255, 255, 255, 0.1);
  color: white;
}

.exam-item:hover {
  transform: translateY(-5px);
  background: rgba(255, 255, 255, 0.2);
  box-shadow: 0 10px 20px rgba(0, 0, 0, 0.2);
}

.exam-info {
  flex: 1;
}

.exam-title {
  margin: 0 0 10px 0;
  font-size: 1.2rem;
  font-weight: 600;
}

.exam-details p {
  margin: 5px 0;
  font-size: 0.9rem;
  opacity: 0.9;
  display: flex;
  align-items: center;
}

.exam-details i {
  margin-right: 8px;
  font-size: 1rem;
}

.exam-actions {
  margin-left: auto;
}

@media (max-width: 768px) {
  .exams-list {
    grid-template-columns: 1fr;
  }
}
</style>